******************
***South Africa***
******************

use "sasas_ddk12.dta", clear

drop if age<=19
drop if age >=60

gen tens = 0
replace tens=1 if age==10 | age == 20 | age == 30 | age == 40 | age == 50 | age == 60| age == 70| age == 80
gen fives = 0
replace five=1 if age==15 | age == 25 | age == 35 | age == 45 | age == 55 | age == 65 | age == 75
gen ones = 0
replace ones=1 if age==11 | age == 21 | age == 31 | age == 41 | age == 51 | age == 61| age == 71| age == 81
gen twos = 0
replace twos=1 if age==12 | age == 22 | age == 32 | age == 42 | age == 52 | age == 62| age == 72| age == 82
gen threes = 0
replace threes=1 if age==13 | age == 23 | age == 33 | age == 43 | age == 53 | age == 63| age == 73| age == 83
gen fours = 0
replace fours=1 if age==14 | age == 24 | age == 34 | age == 44 | age == 54 | age == 64| age == 74| age == 84
gen sixes = 0
replace sixes=1 if age==16 | age == 26 | age == 36 | age == 46 | age == 56 | age == 66| age == 76| age == 86
gen sevens = 0
replace sevens=1 if age==17 | age == 27 | age == 37 | age == 47 | age == 57 | age == 67| age == 77| age == 87
gen eights = 0
replace eights=1 if age==18 | age == 28 | age == 38 | age == 48 | age == 58 | age == 68| age == 78| age == 88
gen nines = 0
replace nines=1 if age==19 | age == 29 | age == 39 | age == 49 | age == 59 | age == 69| age == 79| age == 89

gen twenties = 0
replace twenties = 1 if age>=20 & age<=29
gen thirties = 0
replace thirties = 1 if age>=30 & age<=39
gen forties = 0
replace forties = 1 if age>=40 & age<=49
gen fifties = 0
replace fifties = 1 if age>=50 & age<=59
gen sixties = 0
replace sixties = 1 if age>=60 & age<=69
gen seventies = 0
replace seventies = 1 if age>=70 & age<=79

gen digit = 0
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1

gen zero_or_five = 0
replace zero_or_five = 1 if digit==0 | digit==5

*************
**Figure 2a**
*************

histogram digit, discrete percent ylabel(0(2)20) xtitle(Digit) xlabel(0(1)9)

*****************************
**Myers Index, South Africa**
*****************************

*Generate dataset of ages*

use "sasas_ddk12.dta", clear

drop if age<=19
drop if age >=60

collapse (count) province, by(age)
rename province pop

*Gen last digit variable*
gen lastdigit = mod(age,10)
tab lastdigit [fw=pop]

gen mw = 10
replace mw = age+1 if age<29
replace mw = 59-age if age>49
replace mw = 0 if age>58
gen combow = pop*mw

tab lastdigit [fw=combow], matcell(blended)

*Myers index for South Africa*
myers age [fw=pop], range(20 49)

**************
***Tanzania***
**************

*************************
**Myers Index, Tanzania**
*************************

*Generate dataset of ages*

use "tanzania_plot12.dta", clear

drop if age<=19
drop if age >=60

collapse (count) year, by(age)
rename year pop

*Gen last digit variable*
gen lastdigit = mod(age,10)
tab lastdigit [fw=pop]

gen mw = 10
replace mw = age+1 if age<29
replace mw = 59-age if age>49
replace mw = 0 if age>58
gen combow = pop*mw

tab lastdigit [fw=combow], matcell(blended)

*Myers index for Tanzania*
myers age [fw=pop], range(20 49)

*********************************
**Replicate result from de Kadt**
*********************************

use "tanzania_plot12.dta", clear

rdrandinf vote force, wl(-1) wr(1) reps(10000)

*************
**Figure 2b**
*************

drop if age<=19
drop if age >=60

gen tens = 0
replace tens=1 if age==10 | age == 20 | age == 30 | age == 40 | age == 50 | age == 60| age == 70| age == 80
gen fives = 0
replace five=1 if age==15 | age == 25 | age == 35 | age == 45 | age == 55 | age == 65 | age == 75
gen ones = 0
replace ones=1 if age==11 | age == 21 | age == 31 | age == 41 | age == 51 | age == 61| age == 71| age == 81
gen twos = 0
replace twos=1 if age==12 | age == 22 | age == 32 | age == 42 | age == 52 | age == 62| age == 72| age == 82
gen threes = 0
replace threes=1 if age==13 | age == 23 | age == 33 | age == 43 | age == 53 | age == 63| age == 73| age == 83
gen fours = 0
replace fours=1 if age==14 | age == 24 | age == 34 | age == 44 | age == 54 | age == 64| age == 74| age == 84
gen sixes = 0
replace sixes=1 if age==16 | age == 26 | age == 36 | age == 46 | age == 56 | age == 66| age == 76| age == 86
gen sevens = 0
replace sevens=1 if age==17 | age == 27 | age == 37 | age == 47 | age == 57 | age == 67| age == 77| age == 87
gen eights = 0
replace eights=1 if age==18 | age == 28 | age == 38 | age == 48 | age == 58 | age == 68| age == 78| age == 88
gen nines = 0
replace nines=1 if age==19 | age == 29 | age == 39 | age == 49 | age == 59 | age == 69| age == 79| age == 89

gen twenties = 0
replace twenties = 1 if age>=20 & age<=29
gen thirties = 0
replace thirties = 1 if age>=30 & age<=39
gen forties = 0
replace forties = 1 if age>=40 & age<=49
gen fifties = 0
replace fifties = 1 if age>=50 & age<=59
gen sixties = 0
replace sixties = 1 if age>=60 & age<=69
gen seventies = 0
replace seventies = 1 if age>=70 & age<=79

gen digit = 0
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1

gen zero_or_five = 0
replace zero_or_five = 1 if digit==0 | digit==5

histogram digit, discrete percent ylabel(0(2)20) xtitle(Digit) xlabel(0(1)9)

********************************************
**Reanalyze with reassignment of some ages**
********************************************

set seed 757839

program reanalyze, rclass

use "tanzania_plot12.dta", clear

gen tens = 0
replace tens=1 if age==10 | age == 20 | age == 30 | age == 40 | age == 50 | age == 60| age == 70| age == 80
gen fives = 0
replace five=1 if age==15 | age == 25 | age == 35 | age == 45 | age == 55 | age == 65 | age == 75
gen ones = 0
replace ones=1 if age==11 | age == 21 | age == 31 | age == 41 | age == 51 | age == 61| age == 71| age == 81
gen twos = 0
replace twos=1 if age==12 | age == 22 | age == 32 | age == 42 | age == 52 | age == 62| age == 72| age == 82
gen threes = 0
replace threes=1 if age==13 | age == 23 | age == 33 | age == 43 | age == 53 | age == 63| age == 73| age == 83
gen fours = 0
replace fours=1 if age==14 | age == 24 | age == 34 | age == 44 | age == 54 | age == 64| age == 74| age == 84
gen sixes = 0
replace sixes=1 if age==16 | age == 26 | age == 36 | age == 46 | age == 56 | age == 66| age == 76| age == 86
gen sevens = 0
replace sevens=1 if age==17 | age == 27 | age == 37 | age == 47 | age == 57 | age == 67| age == 77| age == 87
gen eights = 0
replace eights=1 if age==18 | age == 28 | age == 38 | age == 48 | age == 58 | age == 68| age == 78| age == 88
gen nines = 0
replace nines=1 if age==19 | age == 29 | age == 39 | age == 49 | age == 59 | age == 69| age == 79| age == 89

gen twenties = 0
replace twenties = 1 if age>=20 & age<=29
gen thirties = 0
replace thirties = 1 if age>=30 & age<=39
gen forties = 0
replace forties = 1 if age>=40 & age<=49
gen fifties = 0
replace fifties = 1 if age>=50 & age<=59
gen sixties = 0
replace sixties = 1 if age>=60 & age<=69
gen seventies = 0
replace seventies = 1 if age>=70 & age<=79

gen digit = 0
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1

*Generate imputed age variable*
gen newage = age
gen random = runiform() if digit == 0
replace newage = 17 if random<=.075 & age==20
replace newage = 19 if random>.075 & random<=.15 & age==20
replace newage = 21 if random>.15 & random<=.225 & age==20
replace newage = 23 if random>.225 & random<=.3 & age==20
replace newage = 27 if random<=.075 & age==30
replace newage = 29 if random>.075 & random<=.15 & age==30
replace newage = 31 if random>.15 & random<=.225 & age==30
replace newage = 33 if random>.225 & random<=.3 & age==30
replace newage = 37 if random<=.075 & age==40
replace newage = 39 if random>.075 & random<=.15 & age==40
replace newage = 41 if random>.15 & random<=.225 & age==40
replace newage = 43 if random>.225 & random<=.3 & age==40
replace newage = 47 if random<=.075 & age==50
replace newage = 49 if random>.075 & random<=.15 & age==50
replace newage = 51 if random>.15 & random<=.225 & age==50
replace newage = 53 if random>.225 & random<=.3 & age==50
replace newage = 57 if random<=.075 & age==60
replace newage = 59 if random>.075 & random<=.15 & age==60
replace newage = 61 if random>.15 & random<=.225 & age==60
replace newage = 63 if random>.225 & random<=.3 & age==60
replace newage = 67 if random<=.075 & age==70
replace newage = 69 if random>.075 & random<=.15 & age==70
replace newage = 71 if random>.15 & random<=.225 & age==70
replace newage = 73 if random>.225 & random<=.3 & age==70
replace newage = 77 if random<=.075 & age==80
replace newage = 79 if random>.075 & random<=.15 & age==80
replace newage = 81 if random>.15 & random<=.225 & age==80
replace newage = 83 if random>.225 & random<=.3 & age==80

gen random5 = runiform() if digit == 5
replace newage = 33+int((34-33+1)*runiform()) if random5<=.1 & age==25
replace newage = 36+int((37-36+1)*runiform()) if random5>.1 & random5<=.2 & age==25
replace newage = 33+int((34-33+1)*runiform()) if random5<=.1 & age==35
replace newage = 36+int((37-36+1)*runiform()) if random5>.1 & random5<=.2 & age==35
replace newage = 43+int((44-43+1)*runiform()) if random5<=.1 & age==45
replace newage = 46+int((47-46+1)*runiform()) if random5>.1 & random5<=.2 & age==45
replace newage = 53+int((54-53+1)*runiform()) if random5<=.1 & age==55
replace newage = 56+int((57-56+1)*runiform()) if random5>.1 & random5<=.2 & age==55
replace newage = 63+int((64-63+1)*runiform()) if random5<=.1 & age==65
replace newage = 66+int((67-66+1)*runiform()) if random5>.1 & random5<=.2 & age==65
replace newage = 73+int((74-73+1)*runiform()) if random5<=.1 & age==75
replace newage = 76+int((77-76+1)*runiform()) if random5>.1 & random5<=.2 & age==75
replace newage = 83+int((84-83+1)*runiform()) if random5<=.1 & age==85
replace newage = 86+int((87-86+1)*runiform()) if random5>.1 & random5<=.2 & age==85

**Generate new forcing variable from newage**
gen newforce = force - (newage - age)

**Generate new treatment variable based on new force**
gen newtreat = 0
replace newtreat = 1 if newforce >= 1

replace tens=1 if newage==10 | newage == 20 | newage == 30 | newage == 40 | newage == 50 | newage == 60| newage == 70| newage == 80
replace five=1 if newage==15 | newage == 25 | newage == 35 | newage == 45 | newage == 55 | newage == 65 | newage == 75
replace ones=1 if newage==11 | newage == 21 | newage == 31 | newage == 41 | newage == 51 | newage == 61| newage == 71| newage == 81
replace twos=1 if newage==12 | newage == 22 | newage == 32 | newage == 42 | newage == 52 | newage == 62| newage == 72| newage == 82
replace threes=1 if newage==13 | newage == 23 | newage == 33 | newage == 43 | newage == 53 | newage == 63| newage == 73| newage == 83
replace fours=1 if newage==14 | newage == 24 | newage == 34 | newage == 44 | newage == 54 | newage == 64| newage == 74| newage == 84
replace sixes=1 if newage==16 | newage == 26 | newage == 36 | newage == 46 | newage == 56 | newage == 66| newage == 76| newage == 86
replace sevens=1 if newage==17 | newage == 27 | newage == 37 | newage == 47 | newage == 57 | newage == 67| newage == 77| newage == 87
replace eights=1 if newage==18 | newage == 28 | newage == 38 | newage == 48 | newage == 58 | newage == 68| newage == 78| newage == 88
replace nines=1 if newage==19 | newage == 29 | newage == 39 | newage == 49 | newage == 59 | newage == 69| newage == 79| newage == 89

replace twenties = 1 if newage>=20 & newage<=29
replace thirties = 1 if newage>=30 & newage<=39
replace forties = 1 if newage>=40 & newage<=49
replace fifties = 1 if newage>=50 & newage<=59
replace sixties = 1 if newage>=60 & newage<=69
replace seventies = 1 if newage>=70 & newage<=79

replace digit = 0 if tens==1
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1


rdrandinf vote newforce, wl(-1) wr(1) reps(10000) seed(-1)

return scalar estimate = r(obs_stat)

end

simulate estimate=r(estimate), reps(1000): reanalyze

sum, detail

*************
**Figure 3a**
*************

histogram estimate, fraction xaxis(1 2) fcolor(none%0) lcolor(none%0) normal xline(-.04933, lpattern(dash)) xline(-.0516115, lpattern(solid)) xlabel(-.1(.05).1, axis(1)) xscale(noline axis(2)) xlabel(none, noticks axis(2)) xtitle("Estimates") text(.01 -.041 "-.049", size(small)) text(.1 -.039 "Original", size(small)) text(.01 -.06 "-.052", size(small)) text(.1 -.062 "Revised", size(small))

***********
***Kenya***
***********

**********************
**Myers Index, Kenya**
**********************

*Generate dataset of ages*

use "kenya_plot12.dta", clear

drop if age<=19
drop if age >=60

collapse (count) year, by(age)
rename year pop

*Gen last digit variable*
gen lastdigit = mod(age,10)
tab lastdigit [fw=pop]

gen mw = 10
replace mw = age+1 if age<29
replace mw = 59-age if age>49
replace mw = 0 if age>58
gen combow = pop*mw

tab lastdigit [fw=combow], matcell(blended)

*Myers index for Kenya*
myers age [fw=pop], range(20 49)

*********************************
**Replicate result from de Kadt**
*********************************

use "kenya_plot12.dta", clear

rdrandinf vote force, wl(-1) wr(1) reps(10000)

*************
**Figure 2c**
*************

drop if age<=19
drop if age >=60

gen tens = 0
replace tens=1 if age==10 | age == 20 | age == 30 | age == 40 | age == 50 | age == 60| age == 70| age == 80
gen fives = 0
replace five=1 if age==15 | age == 25 | age == 35 | age == 45 | age == 55 | age == 65 | age == 75
gen ones = 0
replace ones=1 if age==11 | age == 21 | age == 31 | age == 41 | age == 51 | age == 61| age == 71| age == 81
gen twos = 0
replace twos=1 if age==12 | age == 22 | age == 32 | age == 42 | age == 52 | age == 62| age == 72| age == 82
gen threes = 0
replace threes=1 if age==13 | age == 23 | age == 33 | age == 43 | age == 53 | age == 63| age == 73| age == 83
gen fours = 0
replace fours=1 if age==14 | age == 24 | age == 34 | age == 44 | age == 54 | age == 64| age == 74| age == 84
gen sixes = 0
replace sixes=1 if age==16 | age == 26 | age == 36 | age == 46 | age == 56 | age == 66| age == 76| age == 86
gen sevens = 0
replace sevens=1 if age==17 | age == 27 | age == 37 | age == 47 | age == 57 | age == 67| age == 77| age == 87
gen eights = 0
replace eights=1 if age==18 | age == 28 | age == 38 | age == 48 | age == 58 | age == 68| age == 78| age == 88
gen nines = 0
replace nines=1 if age==19 | age == 29 | age == 39 | age == 49 | age == 59 | age == 69| age == 79| age == 89

gen twenties = 0
replace twenties = 1 if age>=20 & age<=29
gen thirties = 0
replace thirties = 1 if age>=30 & age<=39
gen forties = 0
replace forties = 1 if age>=40 & age<=49
gen fifties = 0
replace fifties = 1 if age>=50 & age<=59
gen sixties = 0
replace sixties = 1 if age>=60 & age<=69
gen seventies = 0
replace seventies = 1 if age>=70 & age<=79

gen digit = 0
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1

gen zero_or_five = 0
replace zero_or_five = 1 if digit==0 | digit==5

histogram digit, discrete percent ylabel(0(2)20) xtitle(Digit) xlabel(0(1)9)

********************************************
**Reanalyze with reassignment of some ages**
********************************************

set seed 757839

program drop reanalyze

program reanalyze, rclass

use "kenya_plot12.dta", clear

gen tens = 0
replace tens=1 if age==10 | age == 20 | age == 30 | age == 40 | age == 50 | age == 60| age == 70| age == 80
gen fives = 0
replace five=1 if age==15 | age == 25 | age == 35 | age == 45 | age == 55 | age == 65 | age == 75
gen ones = 0
replace ones=1 if age==11 | age == 21 | age == 31 | age == 41 | age == 51 | age == 61| age == 71| age == 81
gen twos = 0
replace twos=1 if age==12 | age == 22 | age == 32 | age == 42 | age == 52 | age == 62| age == 72| age == 82
gen threes = 0
replace threes=1 if age==13 | age == 23 | age == 33 | age == 43 | age == 53 | age == 63| age == 73| age == 83
gen fours = 0
replace fours=1 if age==14 | age == 24 | age == 34 | age == 44 | age == 54 | age == 64| age == 74| age == 84
gen sixes = 0
replace sixes=1 if age==16 | age == 26 | age == 36 | age == 46 | age == 56 | age == 66| age == 76| age == 86
gen sevens = 0
replace sevens=1 if age==17 | age == 27 | age == 37 | age == 47 | age == 57 | age == 67| age == 77| age == 87
gen eights = 0
replace eights=1 if age==18 | age == 28 | age == 38 | age == 48 | age == 58 | age == 68| age == 78| age == 88
gen nines = 0
replace nines=1 if age==19 | age == 29 | age == 39 | age == 49 | age == 59 | age == 69| age == 79| age == 89

gen twenties = 0
replace twenties = 1 if age>=20 & age<=29
gen thirties = 0
replace thirties = 1 if age>=30 & age<=39
gen forties = 0
replace forties = 1 if age>=40 & age<=49
gen fifties = 0
replace fifties = 1 if age>=50 & age<=59
gen sixties = 0
replace sixties = 1 if age>=60 & age<=69
gen seventies = 0
replace seventies = 1 if age>=70 & age<=79

gen digit = 0
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1

*Generate imputed age variable*
gen newage = age
gen random = runiform() if digit == 0
replace newage = 17 if random<=.0875 & age==20
replace newage = 19 if random>.0875 & random<=.175 & age==20
replace newage = 21 if random>.175 & random<=.2625 & age==20
replace newage = 23 if random>.2625 & random<=.35 & age==20
replace newage = 27 if random<=.0875 & age==30
replace newage = 29 if random>.0875 & random<=.175 & age==30
replace newage = 31 if random>.175 & random<=.2625 & age==30
replace newage = 33 if random>.2625 & random<=.35 & age==30
replace newage = 37 if random<=.0875 & age==40
replace newage = 39 if random>.0875 & random<=.175 & age==40
replace newage = 41 if random>.175 & random<=.2625 & age==40
replace newage = 43 if random>.2625 & random<=.35 & age==40
replace newage = 47 if random<=.0875 & age==50
replace newage = 49 if random>.0875 & random<=.175 & age==50
replace newage = 51 if random>.175 & random<=.2625 & age==50
replace newage = 53 if random>.2625 & random<=.35 & age==50
replace newage = 57 if random<=.0875 & age==60
replace newage = 59 if random>.0875 & random<=.175 & age==60
replace newage = 61 if random>.175 & random<=.2625 & age==60
replace newage = 63 if random>.2625 & random<=.35 & age==60
replace newage = 67 if random<=.0875 & age==70
replace newage = 69 if random>.0875 & random<=.175 & age==70
replace newage = 71 if random>.175 & random<=.2625 & age==70
replace newage = 73 if random>.2625 & random<=.35 & age==70
replace newage = 77 if random<=.0875 & age==80
replace newage = 79 if random>.0875 & random<=.175 & age==80
replace newage = 81 if random>.175 & random<=.2625 & age==80
replace newage = 83 if random>.2625 & random<=.35 & age==80

gen random5 = runiform() if digit == 5
replace newage = 33+int((34-33+1)*runiform()) if random5<=.125 & age==25
replace newage = 36+int((37-36+1)*runiform()) if random5>.125 & random5<=.25 & age==25
replace newage = 33+int((34-33+1)*runiform()) if random5<=.125 & age==35
replace newage = 36+int((37-36+1)*runiform()) if random5>.125 & random5<=.25 & age==35
replace newage = 43+int((44-43+1)*runiform()) if random5<=.125 & age==45
replace newage = 46+int((47-46+1)*runiform()) if random5>.125 & random5<=.25 & age==45
replace newage = 53+int((54-53+1)*runiform()) if random5<=.125 & age==55
replace newage = 56+int((57-56+1)*runiform()) if random5>.125 & random5<=.25 & age==55
replace newage = 63+int((64-63+1)*runiform()) if random5<=.125 & age==65
replace newage = 66+int((67-66+1)*runiform()) if random5>.125 & random5<=.25 & age==65
replace newage = 73+int((74-73+1)*runiform()) if random5<=.125 & age==75
replace newage = 76+int((77-76+1)*runiform()) if random5>.125 & random5<=.25 & age==75
replace newage = 83+int((84-83+1)*runiform()) if random5<=.125 & age==85
replace newage = 86+int((87-86+1)*runiform()) if random5>.125 & random5<=.25 & age==85

*Generate new forcing variable from newage*
gen newforce = force - (newage - age)

*Generate new treatment variable based on new force*
gen newtreat = 0
replace newtreat = 1 if newforce >= 1

replace tens=1 if newage==10 | newage == 20 | newage == 30 | newage == 40 | newage == 50 | newage == 60| newage == 70| newage == 80
replace five=1 if newage==15 | newage == 25 | newage == 35 | newage == 45 | newage == 55 | newage == 65 | newage == 75
replace ones=1 if newage==11 | newage == 21 | newage == 31 | newage == 41 | newage == 51 | newage == 61| newage == 71| newage == 81
replace twos=1 if newage==12 | newage == 22 | newage == 32 | newage == 42 | newage == 52 | newage == 62| newage == 72| newage == 82
replace threes=1 if newage==13 | newage == 23 | newage == 33 | newage == 43 | newage == 53 | newage == 63| newage == 73| newage == 83
replace fours=1 if newage==14 | newage == 24 | newage == 34 | newage == 44 | newage == 54 | newage == 64| newage == 74| newage == 84
replace sixes=1 if newage==16 | newage == 26 | newage == 36 | newage == 46 | newage == 56 | newage == 66| newage == 76| newage == 86
replace sevens=1 if newage==17 | newage == 27 | newage == 37 | newage == 47 | newage == 57 | newage == 67| newage == 77| newage == 87
replace eights=1 if newage==18 | newage == 28 | newage == 38 | newage == 48 | newage == 58 | newage == 68| newage == 78| newage == 88
replace nines=1 if newage==19 | newage == 29 | newage == 39 | newage == 49 | newage == 59 | newage == 69| newage == 79| newage == 89

replace twenties = 1 if newage>=20 & newage<=29
replace thirties = 1 if newage>=30 & newage<=39
replace forties = 1 if newage>=40 & newage<=49
replace fifties = 1 if newage>=50 & newage<=59
replace sixties = 1 if newage>=60 & newage<=69
replace seventies = 1 if newage>=70 & newage<=79

replace digit = 0 if tens==1
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1


rdrandinf vote newforce, wl(-1) wr(1) reps(10000) seed(-1)

return scalar estimate = r(obs_stat)

end

simulate estimate=r(estimate), reps(1000): reanalyze

sum, detail

*************
**Figure 3b**
*************

histogram estimate, fraction xaxis(1 2) fcolor(none%0) lcolor(none%0) normal xline(.01961, lpattern(dash)) xline(.0171581, lpattern(solid)) xlabel(-.1(.05).1, axis(1)) xscale(noline axis(2)) xlabel(none, noticks axis(2)) xtitle("Estimates") text(.01 .025 ".02", size(small)) text(.11 .03 "Original", size(small)) text(.01 .01 ".017", size(small)) text(.11 .007 "Revised", size(small))


***********
***Ghana***
***********

**********************
**Myers Index, Ghana**
**********************

*Generate dataset of ages*

use "ghana_plot12.dta", clear

drop if age<=19
drop if age >=60

collapse (count) year, by(age)
rename year pop

*Gen last digit variable*
gen lastdigit = mod(age,10)
tab lastdigit [fw=pop]

gen mw = 10
replace mw = age+1 if age<29
replace mw = 59-age if age>49
replace mw = 0 if age>58
gen combow = pop*mw

tab lastdigit [fw=combow], matcell(blended)

*Myers index for Ghana*
myers age [fw=pop], range(20 49)

*********************************
**Replicate result from de Kadt**
*********************************

use "ghana_plot12.dta", clear

rdrandinf vote force, wl(-1) wr(1) reps(10000)

*************
**Figure 2d**
*************

drop if age<=19
drop if age >=60

gen tens = 0
replace tens=1 if age==10 | age == 20 | age == 30 | age == 40 | age == 50 | age == 60| age == 70| age == 80
gen fives = 0
replace five=1 if age==15 | age == 25 | age == 35 | age == 45 | age == 55 | age == 65 | age == 75
gen ones = 0
replace ones=1 if age==11 | age == 21 | age == 31 | age == 41 | age == 51 | age == 61| age == 71| age == 81
gen twos = 0
replace twos=1 if age==12 | age == 22 | age == 32 | age == 42 | age == 52 | age == 62| age == 72| age == 82
gen threes = 0
replace threes=1 if age==13 | age == 23 | age == 33 | age == 43 | age == 53 | age == 63| age == 73| age == 83
gen fours = 0
replace fours=1 if age==14 | age == 24 | age == 34 | age == 44 | age == 54 | age == 64| age == 74| age == 84
gen sixes = 0
replace sixes=1 if age==16 | age == 26 | age == 36 | age == 46 | age == 56 | age == 66| age == 76| age == 86
gen sevens = 0
replace sevens=1 if age==17 | age == 27 | age == 37 | age == 47 | age == 57 | age == 67| age == 77| age == 87
gen eights = 0
replace eights=1 if age==18 | age == 28 | age == 38 | age == 48 | age == 58 | age == 68| age == 78| age == 88
gen nines = 0
replace nines=1 if age==19 | age == 29 | age == 39 | age == 49 | age == 59 | age == 69| age == 79| age == 89

gen twenties = 0
replace twenties = 1 if age>=20 & age<=29
gen thirties = 0
replace thirties = 1 if age>=30 & age<=39
gen forties = 0
replace forties = 1 if age>=40 & age<=49
gen fifties = 0
replace fifties = 1 if age>=50 & age<=59
gen sixties = 0
replace sixties = 1 if age>=60 & age<=69
gen seventies = 0
replace seventies = 1 if age>=70 & age<=79

gen digit = 0
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1

gen zero_or_five = 0
replace zero_or_five = 1 if digit==0 | digit==5

histogram digit, discrete percent ylabel(0(2)20) xtitle(Digit) xlabel(0(1)9)

********************************************
**Reanalyze with reassignment of some ages**
********************************************

set seed 757839

program drop reanalyze

program reanalyze, rclass

use "ghana_plot12.dta", clear

gen tens = 0
replace tens=1 if age==10 | age == 20 | age == 30 | age == 40 | age == 50 | age == 60| age == 70| age == 80
gen fives = 0
replace five=1 if age==15 | age == 25 | age == 35 | age == 45 | age == 55 | age == 65 | age == 75
gen ones = 0
replace ones=1 if age==11 | age == 21 | age == 31 | age == 41 | age == 51 | age == 61| age == 71| age == 81
gen twos = 0
replace twos=1 if age==12 | age == 22 | age == 32 | age == 42 | age == 52 | age == 62| age == 72| age == 82
gen threes = 0
replace threes=1 if age==13 | age == 23 | age == 33 | age == 43 | age == 53 | age == 63| age == 73| age == 83
gen fours = 0
replace fours=1 if age==14 | age == 24 | age == 34 | age == 44 | age == 54 | age == 64| age == 74| age == 84
gen sixes = 0
replace sixes=1 if age==16 | age == 26 | age == 36 | age == 46 | age == 56 | age == 66| age == 76| age == 86
gen sevens = 0
replace sevens=1 if age==17 | age == 27 | age == 37 | age == 47 | age == 57 | age == 67| age == 77| age == 87
gen eights = 0
replace eights=1 if age==18 | age == 28 | age == 38 | age == 48 | age == 58 | age == 68| age == 78| age == 88
gen nines = 0
replace nines=1 if age==19 | age == 29 | age == 39 | age == 49 | age == 59 | age == 69| age == 79| age == 89

gen twenties = 0
replace twenties = 1 if age>=20 & age<=29
gen thirties = 0
replace thirties = 1 if age>=30 & age<=39
gen forties = 0
replace forties = 1 if age>=40 & age<=49
gen fifties = 0
replace fifties = 1 if age>=50 & age<=59
gen sixties = 0
replace sixties = 1 if age>=60 & age<=69
gen seventies = 0
replace seventies = 1 if age>=70 & age<=79

gen digit = 0
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1

*Generate imputed age variable*
gen newage = age
*Randomly assign some 20, 30, 40, 50 yr olds to be 17/18/19/21/22/23, plus fives, etc.*
gen random = runiform() if digit == 0
replace newage = 17 if random<=.0875 & age==20
replace newage = 19 if random>.0875 & random<=.175 & age==20
replace newage = 21 if random>.175 & random<=.2625 & age==20
replace newage = 23 if random>.2625 & random<=.35 & age==20
replace newage = 27 if random<=.0875 & age==30
replace newage = 29 if random>.0875 & random<=.175 & age==30
replace newage = 31 if random>.175 & random<=.2625 & age==30
replace newage = 33 if random>.2625 & random<=.35 & age==30
replace newage = 37 if random<=.0875 & age==40
replace newage = 39 if random>.0875 & random<=.175 & age==40
replace newage = 41 if random>.175 & random<=.2625 & age==40
replace newage = 43 if random>.2625 & random<=.35 & age==40
replace newage = 47 if random<=.0875 & age==50
replace newage = 49 if random>.0875 & random<=.175 & age==50
replace newage = 51 if random>.175 & random<=.2625 & age==50
replace newage = 53 if random>.2625 & random<=.35 & age==50
replace newage = 57 if random<=.0875 & age==60
replace newage = 59 if random>.0875 & random<=.175 & age==60
replace newage = 61 if random>.175 & random<=.2625 & age==60
replace newage = 63 if random>.2625 & random<=.35 & age==60
replace newage = 67 if random<=.0875 & age==70
replace newage = 69 if random>.0875 & random<=.175 & age==70
replace newage = 71 if random>.175 & random<=.2625 & age==70
replace newage = 73 if random>.2625 & random<=.35 & age==70
replace newage = 77 if random<=.0875 & age==80
replace newage = 79 if random>.0875 & random<=.175 & age==80
replace newage = 81 if random>.175 & random<=.2625 & age==80
replace newage = 83 if random>.2625 & random<=.35 & age==80

gen random5 = runiform() if digit == 5
replace newage = 33+int((34-33+1)*runiform()) if random5<=.15 & age==25
replace newage = 36+int((37-36+1)*runiform()) if random5>.15 & random5<=.3 & age==25
replace newage = 33+int((34-33+1)*runiform()) if random5<=.15 & age==35
replace newage = 36+int((37-36+1)*runiform()) if random5>.15 & random5<=.3 & age==35
replace newage = 43+int((44-43+1)*runiform()) if random5<=.15 & age==45
replace newage = 46+int((47-46+1)*runiform()) if random5>.15 & random5<=.3 & age==45
replace newage = 53+int((54-53+1)*runiform()) if random5<=.15 & age==55
replace newage = 56+int((57-56+1)*runiform()) if random5>.15 & random5<=.3 & age==55
replace newage = 63+int((64-63+1)*runiform()) if random5<=.15 & age==65
replace newage = 66+int((67-66+1)*runiform()) if random5>.15 & random5<=.3 & age==65
replace newage = 73+int((74-73+1)*runiform()) if random5<=.15 & age==75
replace newage = 76+int((77-76+1)*runiform()) if random5>.15 & random5<=.3 & age==75
replace newage = 83+int((84-83+1)*runiform()) if random5<=.15 & age==85
replace newage = 86+int((87-86+1)*runiform()) if random5>.15 & random5<=.3 & age==85

*Generate new forcing variable from newage*
gen newforce = force - (newage - age)

*Generate new treatment variable based on new force*
gen newtreat = 0
replace newtreat = 1 if newforce >= 1

replace tens=1 if newage==10 | newage == 20 | newage == 30 | newage == 40 | newage == 50 | newage == 60| newage == 70| newage == 80
replace five=1 if newage==15 | newage == 25 | newage == 35 | newage == 45 | newage == 55 | newage == 65 | newage == 75
replace ones=1 if newage==11 | newage == 21 | newage == 31 | newage == 41 | newage == 51 | newage == 61| newage == 71| newage == 81
replace twos=1 if newage==12 | newage == 22 | newage == 32 | newage == 42 | newage == 52 | newage == 62| newage == 72| newage == 82
replace threes=1 if newage==13 | newage == 23 | newage == 33 | newage == 43 | newage == 53 | newage == 63| newage == 73| newage == 83
replace fours=1 if newage==14 | newage == 24 | newage == 34 | newage == 44 | newage == 54 | newage == 64| newage == 74| newage == 84
replace sixes=1 if newage==16 | newage == 26 | newage == 36 | newage == 46 | newage == 56 | newage == 66| newage == 76| newage == 86
replace sevens=1 if newage==17 | newage == 27 | newage == 37 | newage == 47 | newage == 57 | newage == 67| newage == 77| newage == 87
replace eights=1 if newage==18 | newage == 28 | newage == 38 | newage == 48 | newage == 58 | newage == 68| newage == 78| newage == 88
replace nines=1 if newage==19 | newage == 29 | newage == 39 | newage == 49 | newage == 59 | newage == 69| newage == 79| newage == 89

replace twenties = 1 if newage>=20 & newage<=29
replace thirties = 1 if newage>=30 & newage<=39
replace forties = 1 if newage>=40 & newage<=49
replace fifties = 1 if newage>=50 & newage<=59
replace sixties = 1 if newage>=60 & newage<=69
replace seventies = 1 if newage>=70 & newage<=79

replace digit = 0 if tens==1
replace digit = 1 if ones==1
replace digit = 2 if twos==1
replace digit = 3 if threes==1
replace digit = 4 if fours==1
replace digit = 5 if fives==1
replace digit = 6 if sixes==1
replace digit = 7 if sevens==1
replace digit = 8 if eights==1
replace digit = 9 if nines==1


rdrandinf vote newforce, wl(-1) wr(1) reps(10000) seed(-1)

return scalar estimate = r(obs_stat)

end

simulate estimate=r(estimate), reps(1000): reanalyze

*************
**Figure 3c**
*************

histogram estimate, fraction xaxis(1 2) fcolor(none%0) lcolor(none%0) normal xline(.05425, lpattern(dash)) xline(.0413533, lpattern(solid)) xlabel(-.1(.05).1, axis(1)) xscale(noline axis(2)) xlabel(none, noticks axis(2)) xtitle("Estimates") text(.01 .062 ".054", size(small)) text(.1 .065 "Original", size(small)) text(.01 .034 ".041", size(small)) text(.1 .03 "Revised", size(small))
